package vn.allmobile.newsreader.adapters;

import java.util.List;

import vn.allmobile.newsreader.R;
import vn.allmobile.newsreader.data.Paper;
import vn.allmobile.newsreader.utils.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class HomeGridViewAdapter extends BaseAdapter {

	private Context mContext;
	private List<Paper> mPapers;

	public HomeGridViewAdapter(Context context, List<Paper> papers) {
		this.mContext = context;
		this.mPapers = papers;
	}

	@Override
	public int getCount() {
		return this.mPapers.size();
	}

	@Override
	public Object getItem(int position) {
		return this.mPapers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View mView = convertView;
		if (mView == null) {
			LayoutInflater li = LayoutInflater.from(mContext);
			mView = li.inflate(R.layout.paper_item, null);
		}
		Paper mPaper = this.mPapers.get(position);
		TextView mTitle = (TextView) mView.findViewById(R.id.grid_item_text);
		mTitle.setText(mPaper.getTitle());

		ImageView mImage = (ImageView) mView.findViewById(R.id.grid_item_image);
		ImageLoader mLoaderImage = new ImageLoader(mContext);
		mLoaderImage.DisplayImage(mPaper.getImage(), mImage);
		return mView;
	}

}
